﻿// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE;
using System.Xml.XPath;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AppConst = WindowsPhoneCommands.Constants;
using Appres = WindowsPhoneCommands.Resources;
using Microsoft.VisualStudio;

// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    internal class EnvPhoneDevice
    {
        internal  static ProjectItem CreateFolderProject(Project project,string localpath)
        {
            var folders = localpath.Split('\\');
            var prjitem = FolderExistProject(project, folders[0]) ?? project.ProjectItems.AddFolder(folders[0]);
            var subf = folders[0];
            for (int i = 1; i < folders.Length; i++)
            {
                subf += "\\" + folders[i];
                var subitem = FolderExistProject(project, subf);
                if (subitem == null)
                {
                    prjitem.ProjectItems.AddFolder(folders[i]);
                }
                else
                {
                    prjitem = FolderExistProject(project, subf);
                }
            }
            return FolderExistProject(project,localpath);
        }

        internal static ProjectItem FolderExistProject(Project project,string nameFolder)
        {
            var directoryInfo = new FileInfo(project.FullName).Directory;
            var root = String.Empty;
            if (directoryInfo != null)
            {
                root = directoryInfo.FullName + "\\";
            }
            var enumprjs = project.ProjectItems.GetEnumerator();
            while (enumprjs.MoveNext())
            {
                var cur = (ProjectItem)enumprjs.Current;
                if (cur.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                {
                    if (cur.Name.ToLower() == nameFolder.ToLower())
                    {
                        return cur;
                    }
                }
                if (cur.ProjectItems != null)
                {
                    var result =  GetExistsubfoldersProject(root,nameFolder,cur.ProjectItems);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            return null;
        }

        private static ProjectItem GetExistsubfoldersProject(string root,string nameFolder, ProjectItems item)
        {
            var enumprjs = item.GetEnumerator();
            while (enumprjs.MoveNext())
            {
                var cur = (ProjectItem)enumprjs.Current;
                if (cur.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                {
                    var localpath = ((ProjectItem)enumprjs.Current).Properties.Item("LocalPath").Value.ToString().Replace(root,"");
                    if (localpath.EndsWith("\\"))
                    {
                        localpath = localpath.Substring(0, localpath.Length - 1);
                    }
                    if (localpath.ToLower() == nameFolder.ToLower())
                    {
                        return cur;
                    }
                }
                if (cur.ProjectItems != null)
                {
                    var result = GetExistsubfoldersProject(root,nameFolder, cur.ProjectItems);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            return null;
        }

        internal static List<string> GetListFolderFromProject(Project project)
        {
            var result = new List<string>();
            var enumprjs = project.ProjectItems.GetEnumerator();
            var directoryInfo = new FileInfo(project.FullName).Directory;
            var root = String.Empty;
            if (directoryInfo != null)
            {
                root = directoryInfo.FullName + "\\";
            }
            while (enumprjs.MoveNext())
            {
                var cur = (ProjectItem)enumprjs.Current;
                if (cur.Kind ==  EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                {
                    var localpath = ((ProjectItem)enumprjs.Current).Properties.Item("LocalPath").Value.ToString();
                    if (localpath.EndsWith("\\"))
                    {
                        localpath = localpath.Substring(0, localpath.Length - 1);
                    }
                    result.Add(localpath.Replace(root, ""));
                    if (cur.ProjectItems != null)
                    {
                        result.AddRange(Getsubfolders(root, cur.ProjectItems));
                    }
                }
            }
            return result;
        }

        private static IEnumerable<string> Getsubfolders(string root, ProjectItems item)
        {
            var result = new List<string>();
            var enumprjs = item.GetEnumerator();
            while (enumprjs.MoveNext())
            {
                var cur = (ProjectItem)enumprjs.Current;
                if (cur.Kind ==  EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                {
                    var localpath = ((ProjectItem)enumprjs.Current).Properties.Item("LocalPath").Value.ToString();
                    if (localpath.EndsWith("\\"))
                    {
                        localpath = localpath.Substring(0, localpath.Length - 1);
                    }
                    result.Add(localpath.Replace(root, ""));
                }
                if (cur.ProjectItems != null)
                {
                    result.AddRange(Getsubfolders(root, cur.ProjectItems));
                }
            }
            return result.ToArray();
        }

        internal static List<string> GetListNeutralResourcesFromProject(Project project)
        {
            var res = GetListFilesFromProject(project, new[] {".resx"});
            //remove resource languages
            var keys = PhoneLanguageSupport().Keys;
            var toremove = res.Where(file => keys.Any(key => file.ToLower().EndsWith("." + key.ToLower() + ".resx"))).ToList();
            foreach (var item in toremove)
            {
                res.Remove(item);
            }
            toremove.Clear();
            toremove.AddRange(res.Where(file => keys.Any(key => file.ToLower().EndsWith("." + key.ToLower().Substring(0, 2) + ".resx"))));
            foreach (var item in toremove)
            {
                res.Remove(item);
            }
            return res;
        }

        internal static List<string> GetListFilesFromProject(Project project, string[] extensions)
        {
            var result = new List<string>();
            var enumprjs = project.ProjectItems.GetEnumerator();
            var directoryInfo = new FileInfo(project.FullName).Directory;
            var root = String.Empty;
            if (directoryInfo != null)
            {
                root = directoryInfo.FullName + "\\";
            }
            while (enumprjs.MoveNext())
            {
                try
                {
                    var fileextension = ((ProjectItem)enumprjs.Current).Properties.Item("Extension").Value.ToString().ToUpper();
                    if (extensions.Any(extfind => fileextension.ToUpper() == extfind.ToUpper()))
                    {
                        var localpath = ((ProjectItem)enumprjs.Current).Properties.Item("LocalPath").Value.ToString();
                        result.Add(localpath.Replace(root, ""));
                    }
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch
                // ReSharper restore EmptyGeneralCatchClause
                {
                }
                if (((ProjectItem)enumprjs.Current).ProjectItems != null)
                {
                    result.AddRange(GetsubItemImages(root, extensions, ((ProjectItem)enumprjs.Current).ProjectItems));
                }
            }
            return result;
        }

        private static IEnumerable<string> GetsubItemImages(string root, string[] extensions, ProjectItems item)
        {
            var result = new List<string>();
            var enumprjs = item.GetEnumerator();
            while (enumprjs.MoveNext())
            {
                try
                {
                    var fileextension = ((ProjectItem)enumprjs.Current).Properties.Item("Extension").Value.ToString().ToUpper();
                    if (extensions.Any(extfind => fileextension.ToUpper() == extfind.ToUpper()))
                    {
                        var localpath = ((ProjectItem)enumprjs.Current).Properties.Item("LocalPath").Value.ToString();
                        result.Add(localpath.Replace(root, ""));
                    }
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch
                // ReSharper restore EmptyGeneralCatchClause
                {
                }
                if (((ProjectItem)enumprjs.Current).ProjectItems != null)
                {
                    result.AddRange(GetsubItemImages(root, extensions, ((ProjectItem)enumprjs.Current).ProjectItems));
                }
            }
            return result.ToArray();
        }

        internal static void UpdateCacheApp(string name, string alias, string guid)
        {
            var item = WpIsolateSaveData.Current.WpApplication.FirstOrDefault(a => a.Guid == guid);
            if (item != null)
            {
                item.Title = name;
                item.RenameTitle = alias;
            }
            else
            {
                item = new WpDeployApplication { Guid = guid, Title = name, RenameTitle = alias };
            }
            WpIsolateSaveData.Current.AddData(item);
        }

        internal static WmAppManifest ReadWmAppManifest(DTE2 dte, string name)
        {
            var proj = FindProjectByName(dte, name);
            if (proj != null)
            {
                var path = GetPathFileManifest(proj);
                if (!string.IsNullOrEmpty(path))
                {
                    try
                    {
                        //var memStr = new MemoryStream(Encoding.UTF8.GetBytes(path));
                        var xpathDoc = new XPathDocument(path);
                        //memStr.Close();
                        var navigator = xpathDoc.CreateNavigator().SelectSingleNode("//App");
                        if (navigator != null)
                        {
                            // ReSharper disable UseObjectOrCollectionInitializer
                            var result = new WmAppManifest();
                            // ReSharper restore UseObjectOrCollectionInitializer
                            result.Title = Common.XmlUnescape(navigator.GetAttribute("Title", string.Empty).Trim());
                            result.ProductId = Common.XmlUnescape(navigator.GetAttribute("ProductID", string.Empty).Replace("{", "").Replace("}", "").Trim());
                            result.Author = Common.XmlUnescape(navigator.GetAttribute("Author", string.Empty).Trim());
                            result.Description = Common.XmlUnescape(navigator.GetAttribute("Description", string.Empty).Trim());
                            result.Publisher = Common.XmlUnescape(navigator.GetAttribute("Publisher", string.Empty).Trim());
                            result.Version = Common.XmlUnescape(navigator.GetAttribute("Version", string.Empty).Trim());
                            result.HasSettings = Common.XmlBoolConvert(navigator.GetAttribute("HasSettings", string.Empty), false, true);
                            result.IsBeta = Common.XmlBoolConvert(navigator.GetAttribute("IsBeta", string.Empty), false, true);
                            result.SingleInst = Common.XmlBoolConvert(navigator.GetAttribute("SingleInstanceHost", string.Empty), false, true);

                            var selectSingleNode = navigator.SelectSingleNode("//App//IconPath");
                            if (selectSingleNode != null)
                            {
                                result.IconApp = Common.XmlUnescape(selectSingleNode.ToString());
                            }
                            selectSingleNode = navigator.SelectSingleNode("//App//Capabilities");
                            if (selectSingleNode != null && selectSingleNode.HasChildren)
                            {
                                var capabilitiesNodes = selectSingleNode.SelectChildren("", string.Empty);
                                while (capabilitiesNodes.MoveNext())
                                {
                                    if (capabilitiesNodes.Current == null) continue;
                                    var cap = capabilitiesNodes.Current.GetAttribute("Name", string.Empty);
                                    switch (cap)
                                    {
                                        case AppConst.IdCapAppointments:
                                            result.CapAppointments = true;
                                            break;
                                        case AppConst.IdCapContacts:
                                            result.CapContacts = true;
                                            break;
                                        case AppConst.IdCapGamerservices:
                                            result.CapGamerSrv = true;
                                            break;
                                        case AppConst.IdCapIdentityDevice:
                                            result.CapIdentDevive = true;
                                            break;
                                        case AppConst.IdCapIdentityUser:
                                            result.CapIdentUser = true;
                                            break;
                                        case AppConst.IdCapIsvCamera:
                                            result.CapCamera = true;
                                            break;
                                        case AppConst.IdCapLocation:
                                            result.CapLocation = true;
                                            break;
                                        case AppConst.IdCapMedialib:
                                            result.CapMediaLib = true;
                                            break;
                                        case AppConst.IdCapMicrophone:
                                            result.CapMicrophone = true;
                                            break;
                                        case AppConst.IdCapNetworking:
                                            result.CapNetworking = true;
                                            break;
                                        case AppConst.IdCapPhonedialer:
                                            result.CapPhoneDialer = true;
                                            break;
                                        case AppConst.IdCapPushNotification:
                                            result.CapPushNotif = true;
                                            break;
                                        case AppConst.IdCapSensors:
                                            result.CapSensor = true;
                                            break;
                                        case AppConst.IdCapWebbrowsercomponent:
                                            result.CapWebBrowser = true;
                                            break;
                                    }
                                }
                            }
                            selectSingleNode = navigator.SelectSingleNode("//App//Tasks//DefaultTask");
                            if (selectSingleNode != null)
                            {
                                result.StartPage = Common.XmlUnescape(selectSingleNode.GetAttribute("NavigationPage", string.Empty));
                            }
                            selectSingleNode = navigator.SelectSingleNode("//App//Tokens//PrimaryToken");
                            if (selectSingleNode != null)
                            {
                                result.TokenId = Common.XmlUnescape(selectSingleNode.GetAttribute("TokenID", string.Empty));
                            }
                            selectSingleNode = navigator.SelectSingleNode("//App//Tokens//PrimaryToken//TemplateType5//BackgroundImageURI");
                            if (selectSingleNode != null)
                            {
                                result.TileBackground = Common.XmlUnescape(selectSingleNode.Value);
                            }
                            selectSingleNode = navigator.SelectSingleNode("//App//Tokens//PrimaryToken//TemplateType5//Count");
                            if (selectSingleNode != null)
                            {
                                result.TileCount = Common.XmlUnescape(selectSingleNode.Value);
                            }
                            selectSingleNode = navigator.SelectSingleNode("//App//Tokens//PrimaryToken//TemplateType5//Title");
                            if (selectSingleNode != null)
                            {
                                result.TileTitle = Common.XmlUnescape(selectSingleNode.Value);
                            }
                            return result;
                        }
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
            return null;
        }

        internal static event EventHandler<EventArgs> ChangedTargetDevice;

        internal static string CurrentTargetDevice
        {
            get
            {
                return GetCurrentWpComboText();
            }
        }

        internal static CommandBarComboBox CommandBarTargetDeviceComboBox;

        internal static void ListenVsTargetDevice(DTE2 dte)
        {
            foreach (CommandBar commandBar in (_CommandBars)dte.CommandBars)
            {
                // ReSharper disable LoopCanBeConvertedToQuery
                foreach (var obj in commandBar.Controls)
                {
                    var commandBarComboBox = obj as CommandBarComboBox;
                    if (commandBarComboBox != null && commandBarComboBox.accName[Missing.Value] == "Target Deployment Device")
                    {
                        string result = GetCurrentWpComboText();
                        if (result != null)
                        {
                            CommandBarTargetDeviceComboBox = obj as CommandBarComboBox;
                            CommandBarTargetDeviceComboBox.Change += CommandBarTargetDeviceComboBoxChange;
                        }
                        break;
                    }
                }
                // ReSharper restore LoopCanBeConvertedToQuery
            }
        }

        internal static string PhoneVersion
        {
            get
            {
                if (WPhoneDevice.IsConnected)
                {
                    return new Version(WPhoneDevice.SystemInfo.OSMajor, WPhoneDevice.SystemInfo.OSMinor).ToString();
                }
                return string.Empty;
            }
        }

        private const string DefaultnameManifestWp = "WMAppManifest.xml";

        private static void CommandBarTargetDeviceComboBoxChange(CommandBarComboBox ctrl)
        {
            if (ChangedTargetDevice != null)
            {
                var tmp = ChangedTargetDevice;
                tmp(null, new EventArgs());
            }
        }

        private static string GetCurrentWpComboText()
        {
            var oleCommandTarget = (Microsoft.VisualStudio.OLE.Interop.IOleCommandTarget)Package.GetGlobalService(typeof(SUIHostCommandDispatcher));
            if (oleCommandTarget != null)
            {
                var pguidCmdGroup = Guid.Parse("{04151D39-35B6-4E53-BEEE-48DF3BB8CFE5}");
                const uint nCmdId = 257U;
                var num = Marshal.AllocCoTaskMem(16);
                if (num != IntPtr.Zero)
                {
                    try
                    {
                        Marshal.WriteInt16(num, 8);
                        if (oleCommandTarget.Exec(ref pguidCmdGroup, nCmdId, 0U, IntPtr.Zero, num) == 0)
                            return (string)Marshal.GetObjectForNativeVariant(num);
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(num);
                    }
                }
            }
            return string.Empty;
        }

        internal static ProjectItem GetManifestWp(ProjectItem item)
        {
            if (item.Name.ToLower().Contains(DefaultnameManifestWp.ToLower()))
            {
                return item;
            }
            if (item.ProjectItems == null)
            {
                return null;
            }
            var subitems = item.ProjectItems.GetEnumerator();
            while (subitems.MoveNext())
            {
                var result = GetManifestWp((ProjectItem)subitems.Current);
                if (result != null)
                {
                    return result;
                }
            }
            return null;
        }

        internal static string GetPathFileManifest(Project project)
        {
            var result = GetProjectManifestWp(project);
            return result != null ? result.Properties.Item("FullPath").Value.ToString() : null;
        }

        internal static string GetNeutralResourcesLanguage(Project project)
        {
            return project.Properties.Item("NeutralResourcesLanguage").Value.ToString();
        }

        internal static bool GetIsAppPhone(Project project)
        {
            return (bool)project.Properties.Item("WindowsPhoneProject.IsWindowsPhoneApplication").Value;
        }

        internal static string GetSupportedCultures(Project project)
        {
            var file = project.FullName;
            var result = string.Empty;
            string xmlproject;
            using (var ioFile = new StreamReader(file))
            {
                xmlproject = ioFile.ReadToEnd();
            }
            var startindex = xmlproject.IndexOf("<SupportedCultures>", StringComparison.OrdinalIgnoreCase);
            if (startindex > 0)
            {
                var endindex = xmlproject.IndexOf("</SupportedCultures>", StringComparison.OrdinalIgnoreCase);
                result = xmlproject.Substring(startindex, endindex - startindex);
                result = result.Replace("<SupportedCultures>", "").Replace("\r", "").Replace("\n", "").Trim();
            }
            return result;
        }

        internal static string GetValuePhoneLanguageSupport(string key)
        {
            if (key == null) return null;
            return PhoneLanguageSupport().ContainsKey(key) ? PhoneLanguageSupport()[key] : null;
        }

        internal static string GetKeyPhoneLanguageSupport(string value)
        {
            var tmp = PhoneLanguageSupport().FirstOrDefault(l => l.Value == value);
            return tmp.Value == value?tmp.Key:null;
        }

        private static Dictionary<string, string> _phoneLanguageSupport;
        internal static Dictionary<string,string> PhoneLanguageSupport()
        {
            return _phoneLanguageSupport ?? (_phoneLanguageSupport = new Dictionary<string, string>
                    {
                        {"zh-CN",Appres.LngzhCN},
                        {"zh-TW", Appres.LngzhTW},
                        {"cs-CZ", Appres.LngcsCZ},
                        {"da-DK", Appres.LngDaDK},
                        {"nl-NL", Appres.LngnlNL},
                        {"en-US", Appres.LngenUS},
                        {"en-GB", Appres.LngenGB},
                        {"fi-FI", Appres.LngfiFI},
                        {"fr-FR", Appres.LngfrFR},
                        {"de-DE", Appres.LngdeDE},
                        {"el-GR", Appres.LngelGR},
                        {"hu-HU", Appres.LnghuHU},
                        {"id-ID", Appres.LngidID},
                        {"it-IT", Appres.LngitIT},
                        {"ja-JP", Appres.LngjaJP},
                        {"ko-KR", Appres.LngkoKR},
                        {"ms-MY", Appres.LngmsMY},
                        {"nb-NO", Appres.LngnbNO},
                        {"pl-PL", Appres.LngplPL},
                        {"pt-BR", Appres.LngptBR},
                        {"pt-PT", Appres.LngptPT},
                        {"ru-RU", Appres.LngruRU},
                        {"es-ES", Appres.LngesES},
                        {"sv-SE", Appres.LngsvSE}
                    });
        }

        internal static Project FindProjectByName(DTE2 dte, string name)
        {
            Project found = null;
            if (dte != null)
            {
                var enumprjs = dte.Solution.Projects.GetEnumerator();
                while (enumprjs.MoveNext())
                {
                    if (((Project)enumprjs.Current).Name == name)
                    {
                        found = (Project)enumprjs.Current;
                        break;
                    }
                }
            }
            return found;
        }

        internal static Project GetCurrentProject(DTE2 dte)
        {
            foreach (SelectedItem item in dte.SelectedItems)
            {
                if (item.Project != null) return item.Project;
                if (item.ProjectItem != null) return item.ProjectItem.ContainingProject;
            }
            return null;
        }

        internal static ProjectItem GetProjectManifestWp(Project project)
        {
            if (project.ProjectItems == null) return null;

            var subitems = project.ProjectItems.GetEnumerator();
            ProjectItem result = null;
            while (subitems.MoveNext())
            {
                result = GetManifestWp((ProjectItem)subitems.Current);
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }

        internal static Guid GetProjectGuid(IVsSolution solution, Project project)
        {

            IVsHierarchy hierarchy;

            solution.GetProjectOfUniqueName(project.FullName, out hierarchy);

            if (hierarchy != null)
            {
                Guid projectGuid;

                ErrorHandler.Succeeded(
                    hierarchy.GetGuidProperty(
                        VSConstants.VSITEMID_ROOT,
                        (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
                        out projectGuid));

                if (projectGuid != new Guid())
                {
                    return projectGuid;
                }
            }

            return Guid.Empty;
        }

        internal static bool IsPhoneProject(IVsSolution solution, Project project)
        {
            IVsHierarchy hierarchy;
            solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
            return IsPhoneProject(hierarchy);
        }
        internal static bool IsPhoneProject(IVsHierarchy pHierarchy)
        {
            try
            {
                var aggregatableProject = (IVsAggregatableProject)pHierarchy;
                string projectTypeGuids;
                aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids);
                //windows phone C#/VB Template Project
                if (projectTypeGuids.ToUpper().Contains("{C089C8C0-30E0-4E22-80C0-CE093F111A43}") || projectTypeGuids.Contains("{DB03555F-0C8B-43BE-9FF9-57896B3C5E56}"))
                {
                    return true;
                }
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch { }
            // ReSharper restore EmptyGeneralCatchClause
            return false;
        }

        internal static bool IsVbProject(IVsSolution solution, Project project)
        {
            IVsHierarchy hierarchy;
            solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
            return IsVbProject(hierarchy);
        }
        internal static bool IsVbProject(IVsHierarchy pHierarchy)
        {
            try
            {
                var aggregatableProject = (IVsAggregatableProject)pHierarchy;
                string projectTypeGuids;
                aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids);
                //windows phone C#/VB Template Project
                if (projectTypeGuids.ToUpper().Contains("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}"))
                {
                    return true;
                }
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch { }
            // ReSharper restore EmptyGeneralCatchClause
            return false;
        }

        internal static bool IsCSharpProject(IVsSolution solution, Project project)
        {
            IVsHierarchy hierarchy;
            solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
            return IsCSharpProject(hierarchy);
        }
        internal static bool IsCSharpProject(IVsHierarchy pHierarchy)
        {
            try
            {
                var aggregatableProject = (IVsAggregatableProject)pHierarchy;
                string projectTypeGuids;
                aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids);
                //windows phone C#/VB Template Project
                if (projectTypeGuids.ToUpper().Contains("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"))
                {
                    return true;
                }
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch { }
            // ReSharper restore EmptyGeneralCatchClause
            return false;
        }

        internal static bool IsValidProject(IVsHierarchy pHierarchy)
        {
            try
            {
                // ReSharper disable UnusedVariable
                var dummy = (IVsAggregatableProject)pHierarchy;
                // ReSharper restore UnusedVariable
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch
            {
                return false;
            }
            // ReSharper restore EmptyGeneralCatchClause
            return true;
        }




    }
}
